﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class HangGiaCong_DAO
    {
        private static HangGiaCong_DAO m_instance = null;

        public static HangGiaCong_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new HangGiaCong_DAO();
                return m_instance;
            }
        }
        
        /// <summary>
        ///   lay toan bo ma hang gia cong
        /// </summary>
        /// 

        public DataTable LayToanBoMaHangGiaCong()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTOANBOMAHANGGIACONG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        ///   lay thong tin hang gia cong bang id
        /// </summary>
        /// 

        public DataTable LayThongTinHangGiaCong(string _id)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYHANGGIACONGBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@mahanggiacong", SqlDbType.NVarChar, 10);
                cmd.Parameters["@mahanggiacong"].Value = _id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   Lay Toàn bộ hàng gia công
        /// </summary>
        /// 

        public DataTable LayToanBoHangGiaCong()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYHANGGIACONG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   thêm hàng gia công mới
        /// </summary>
        /// 

        public bool ThemHangGiaCongMoi(string _name, long _price)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_HANGGIACONG_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@tenhanggiacong", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tenhanggiacong"].Value = _name;

                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = _price;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///   Sửa Hàng Gia Công
        /// </summary>
        /// 


        public bool SuaHangGiaCong(string _id, string _name, long _price)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_HANGGIACONG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.Add("@mahanggiacong", SqlDbType.NVarChar, 10);
                cmd.Parameters["@mahanggiacong"].Value = _id;

                cmd.Parameters.Add("@tenhanggiacong", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tenhanggiacong"].Value = _name;

                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = _price;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        ///   xóa hàng gia công
        /// </summary>
        /// 

        public bool XoaHangGiaCong(string _id)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_HANGGIACONG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@mahanggiacong", SqlDbType.NVarChar, 10);
                cmd.Parameters["@mahanggiacong"].Value = _id;


                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

    }
}
